﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace BlackBox.Models
{
	public class UserRepository
	{
        DataClassesDataContext db = new DataClassesDataContext();

        #region Get All users
        public IEnumerable<UserProfiles> GetAllUsers()
		{
			var users = (from u in db.UserProfiles
						 select u);
           
            return users;
		}
        #endregion

        #region Get By Id 
        public UserProfiles GetByID(int id)
		{
			var tempUser = (from u in db.UserProfiles
						  where u.UserId == id
						 select u).SingleOrDefault();

			return tempUser;
		}
        #endregion

        #region Get by Name
        public UserProfiles GetByName(string name)
		{
			var myUser = (from u in db.UserProfiles
						  where u.UserName == name
                          select u).SingleOrDefault();

			return myUser;
		}

        #endregion

        #region Add User
        public void AddUser(UserProfiles newUser)
		{
            //Creating the new user
			UserProfiles myUser = new UserProfiles
            {
				UserId = newUser.UserId,
		        UserName = newUser.UserName,
				About = newUser.About,
				Email = newUser.Email,
                Img = newUser.Img
			};
			db.UserProfiles.InsertOnSubmit(myUser);
			db.SubmitChanges();
		}
        #endregion

        #region Remove User
        public void RemoveUser(UserProfiles oldUser)
        {
            var deleteUser = (from x in db.UserProfiles
                              where x.UserId == oldUser.UserId
                              select x).SingleOrDefault();

            var deleteUserScore = (from x in db.ScoreToUserToGames
                                   where x.userid == oldUser.UserId
                                   select x);

            var deleteUserGroups = (from x in db.UserToGroups
                                    where x.UserID == oldUser.UserId
                                    select x);
            
            //var deleteUserRole = (from x in db.UsersInRoles
            //                      where x.UserId == oldUser.UserId
            //                      select x).Single();

            /*var deleteUserRatings = (from x in db.RatingToUserToGames
                                     where x.UserId == oldUser.UserId
                                     select x);*/

            db.UserProfiles.DeleteOnSubmit(deleteUser);

            foreach (var score in deleteUserScore)
            {
                db.ScoreToUserToGames.DeleteOnSubmit(score);
            }

            foreach (var group in deleteUserGroups)
            {
                db.UserToGroups.DeleteOnSubmit(group);
            }

            //db.UsersInRoles.DeleteOnSubmit(deleteUserRole);

            /*foreach(var rating in deleteUserRatings)
            {
                db.RatingToUserToGames.DeleteOnSubmit(rating);
            }*/

            db.SubmitChanges();
        }
        #endregion

        #region Update User
        public void UpdateUser(UserProfiles newUser)
		{
			UserProfiles oldUser = (from u in db.UserProfiles
							where u.UserId == newUser.UserId
							select u).SingleOrDefault();

            oldUser.Email = newUser.Email;
            oldUser.About = newUser.About;
            oldUser.Img = newUser.Img;

            
			db.SubmitChanges();
		}
        #endregion

        #region Get User by Group
        public List<UserProfiles> GetUsersByGroup(int id)
		{
			var myUserID = (from x in db.UserToGroups
							where x.GroupID == id
							select x);

            //Converting to List
			List<UserProfiles> myUsers = new List<UserProfiles>();

			foreach (var newUser in myUserID)
			{
				UserProfiles temp = GetByID(newUser.UserID);
				myUsers.Add(temp);
			}

			return myUsers;
		}
        #endregion

        #region Invite
            #region Add Invite
            public void AddInvite(Invite myInvite)
		    {
			    db.Invites.InsertOnSubmit(myInvite);
			    db.SubmitChanges();
		    }
            #endregion

            #region Invite Accepted
            public void InviteAccepted(Invite myInvite)
		    {
			    IEnumerable<Invite> acceptedInvites = (from x in db.Invites
							       where x.UserID == myInvite.UserID
							       && x.GroupID == myInvite.GroupID
							       && x.GameID == myInvite.GameID
							       select x);
			    foreach (var accept in acceptedInvites)
			    {
				    db.Invites.DeleteOnSubmit(accept);
			    }
			    db.SubmitChanges();
		    }
            #endregion

            #region Get Invites By User
            public IEnumerable<Invite> GetInvitesByUser(int id)
		    {
			    var model = (from x in db.Invites
						     where x.UserID == id
						     select x);
			    return model;
            }
            #endregion

        #endregion
    }
}